import json
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By

from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.kuaile8_vo import Kuaile8Model, Kuaile8PageQueryModel, DeleteKuaile8Model
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.kuaile8_service import Kuaile8Service
from module_admin.service.login_service import LoginService

from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


# kuaile8Controller = APIRouter(prefix='/business/kuaile8', dependencies=[Depends(LoginService.get_current_user)])
kuaile8Controller = APIRouter(prefix='/business/kuaile8')

@kuaile8Controller.get(
    '/get_kuaile8_list', response_model=PageResponseModel
)
async def get_business_kuaile8_datalist(
    request: Request,
kuaile8_page_query: Kuaile8PageQueryModel = Depends(Kuaile8PageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    kuaile8_page_query_result = await Kuaile8Service.get_kuaile8_list_services(query_db, kuaile8_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=kuaile8_page_query_result)


@kuaile8Controller.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('business:kuaile8:list'))]
)
async def get_business_kuaile8_list(
    request: Request,
kuaile8_page_query: Kuaile8PageQueryModel = Depends(Kuaile8PageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    kuaile8_page_query_result = await Kuaile8Service.get_kuaile8_list_services(query_db, kuaile8_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=kuaile8_page_query_result)


@kuaile8Controller.post('', dependencies=[Depends(CheckUserInterfaceAuth('business:kuaile8:add'))])
@ValidateFields(validate_model='add_kuaile8')
@Log(title='kuaile8中奖号码查询', business_type=BusinessType.INSERT)
async def add_business_kuaile8(
    request: Request,
    add_kuaile8: Kuaile8Model,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_kuaile8_result = await Kuaile8Service.add_kuaile8_services(query_db, add_kuaile8)
    logger.info(add_kuaile8_result.message)

    return ResponseUtil.success(msg=add_kuaile8_result.message)


@kuaile8Controller.put('', dependencies=[Depends(CheckUserInterfaceAuth('business:kuaile8:edit'))])
@ValidateFields(validate_model='edit_kuaile8')
@Log(title='kuaile8中奖号码查询', business_type=BusinessType.UPDATE)
async def edit_business_kuaile8(
    request: Request,
    edit_kuaile8: Kuaile8Model,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_kuaile8.update_by = current_user.user.user_name
    edit_kuaile8.update_time = datetime.now()
    edit_kuaile8_result = await Kuaile8Service.edit_kuaile8_services(query_db, edit_kuaile8)
    logger.info(edit_kuaile8_result.message)

    return ResponseUtil.success(msg=edit_kuaile8_result.message)


@kuaile8Controller.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('business:kuaile8:remove'))])
@Log(title='kuaile8中奖号码查询', business_type=BusinessType.DELETE)
async def delete_business_kuaile8(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_kuaile8 = DeleteKuaile8Model(ids=ids)
    delete_kuaile8_result = await Kuaile8Service.delete_kuaile8_services(query_db, delete_kuaile8)
    logger.info(delete_kuaile8_result.message)

    return ResponseUtil.success(msg=delete_kuaile8_result.message)


@kuaile8Controller.get(
    '/{id}', response_model=Kuaile8Model, dependencies=[Depends(CheckUserInterfaceAuth('business:kuaile8:query'))]
)
async def query_detail_business_kuaile8(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    kuaile8_detail_result = await Kuaile8Service.kuaile8_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=kuaile8_detail_result)


@kuaile8Controller.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('business:kuaile8:export'))])
@Log(title='kuaile8中奖号码查询', business_type=BusinessType.EXPORT)
async def export_business_kuaile8_list(
    request: Request,
    kuaile8_page_query: Kuaile8PageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    kuaile8_query_result = await Kuaile8Service.get_kuaile8_list_services(query_db, kuaile8_page_query, is_page=False)
    kuaile8_export_result = await Kuaile8Service.export_kuaile8_list_services(kuaile8_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(kuaile8_export_result))
